package org.example.user.manage.repository;

import org.example.commons.entity.SysAdmin;
import org.example.commons.entity.SysHealthRecord;
import org.example.commons.entity.SysHealthStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 住户健康状态 数据层
 *
 * @author bothin
 */
@Repository
public interface SysHealthStatusRepository extends JpaRepository<SysHealthStatus,Long> {

    SysHealthStatus findByUserId(Long userId);

    List<SysHealthStatus> findByStatusIdIn(Long[] ids);

    /**
     * 条件查询
     * @return
     */
    @Query(value = "select status from SysHealthStatus status " +
            "left join SysUser u on u.userId = status.userId " +
            "where u.name like :name  " +
            " and (:healthStatus IS NULL or status.healthStatus = :healthStatus ) " +
            " and ( :#{#dates[0]} IS NULL OR :#{#dates[1]} IS NULL " +
            " OR status.updateTime between :#{#dates[0]} and :#{#dates[1]})" )
    Page<SysHealthStatus> searchList(String name,  Integer healthStatus, Date[] dates, Pageable pageable);

    @Query(value = "SELECT * FROM sys_health_status  where DATE_SUB(CURDATE(), INTERVAL ? DAY) >= date(update_time)",nativeQuery =true)
    List<SysHealthStatus>  findLastSysHealthStatus(Integer days);


}
